package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.mapper.ClassinfoMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IClassinfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//  使用注解创建对象
@Service
public class ClassinfoServiceImpl implements IClassinfoService{

    //  获取容器中的对象并注入Bean
    @Autowired
    private ClassinfoMapper classinfoMapper;

    @Override
    public void deleteById(Long id) {
        classinfoMapper.deleteById(id);
    }

    @Override
    public void insert(Classinfo classinfo) {
        classinfoMapper.insert(classinfo);
    }

    @Override
    public Classinfo selectById(Long id) {
        return classinfoMapper.selectById(id);
    }

    @Override
    public List<Classinfo> selectAll() {
        return classinfoMapper.selectAll();
    }

    @Override
    public void updateById(Classinfo classinfo) {
        classinfoMapper.updateById(classinfo);
    }

    @Override
    public PageInfo<Classinfo> query(QueryObject qo) {
        /*
        //  查询满足条件的总条数
        int totalCount = classinfoMapper.queryForCount(qo);
        //  判断是否为零
        if (totalCount == 0) {
            //  返回一个结果集
            return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), totalCount, Collections.emptyList());
        }

        int totalPage = totalCount % qo.getPageSize() == 0 ?
                totalCount / qo.getPageSize() : totalCount / qo.getPageSize() + 1;
        if (qo.getCurrentPage() > totalPage) {
            qo.setCurrentPage(totalPage);
        }*/

        //  当前页，每页显示多少条
        //  会对下一条sql进行拦截，加上limit过滤
        //  若有其他需求，此方法后必须跟上queryForList方法
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());

        //  查询满足条件的每页显示的数据
        List<Classinfo> classinfos = classinfoMapper.queryForList(qo);
        //  返回一个结果集
        return new PageInfo<>(classinfos);
    }
}
